﻿namespace AutonomousView
{
    using System.Windows;
    using System.Windows.Controls;

    public partial class RegistrationForm : Window
    {
        private bool _isFormValid;

        public RegistrationForm()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            ExecuteRegistration();
        }

        private void Grid_TextChanged(object sender, TextChangedEventArgs e)
        {
            _isFormValid = true;
            ValidationSummary.Items.Clear();

            if (string.IsNullOrEmpty(Email.Text))
                ReportProblem("Email cannot be blank.");

            if (string.IsNullOrEmpty(Password.Text))
                ReportProblem("Password cannot be blank.");

            if (Password.Text != Confirm.Text)
                ReportProblem("Password and confirmation do not match.");

            RegistrationButton.IsEnabled = _isFormValid;
        }

        private void ReportProblem(string reason)
        {
            _isFormValid = false;
            ValidationSummary.Items.Add(reason);
        }

        private void ExecuteRegistration()
        {
            if (!_isFormValid) return;

            var service = new RegistrationService();
            service.Register(Email.Text, Password.Text);
        }
    }
}